package com.atguigu.chapter05;

import com.atguigu.chapter02.Event;
import org.apache.flink.streaming.api.functions.source.SourceFunction;

import java.util.Calendar;
import java.util.Random;
//生成Event数据
public class Clicksource implements SourceFunction<Event> {
    private boolean running=true;
    @Override
    public void run(SourceContext<Event> sourceContext) throws Exception {
        Random random = new Random();
        String[] users={"Mary","Alice","Bob","Cary"};
        String [] urls={"./home","./cart","./fav","./prod?id=1","./prod?id=2"};

        while (running){
            sourceContext.collect(new Event(users[random.nextInt(users.length)],
                                        urls[random.nextInt(urls.length)],
                    Calendar.getInstance().getTimeInMillis()));
            Thread.sleep(1000);
        }

    }
    //控制程序的运行
    @Override
    public void cancel() {
        running=false;
    }
}
